<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/3/2
 * Time: 22:30
 */
namespace Admin\Controller;

class CommunityController extends AdminController
{
    protected $searchRule = array(
        'no' => array('/^\d+$/','eq'),
        'name' => array('/^[\x{4e00}-\x{9fa5}A-Za-z0-9_]+$/u','like','%','%'),
        'full_parent_name' => array('/^[\x{4e00}-\x{9fa5}A-Za-z0-9_]+$/u','like','%','%'),
        'address' => array('/^[\x{4e00}-\x{9fa5}A-Za-z0-9_]+$/u','like','%','%')
    );

    public function search(){
        $param = $this->getParam();
        $modelName = $this->modelName;
        $scope = array(
            'order' => array('no'),
        );
        $relation = array();
        $search = $this->getSearch();
        $page = $this->getPage();
        $data = D($modelName)->where($param)->where(array_merge($search,array('status'=>array('neq',-1))))->scope($scope)->relation($relation)->page($page[0],$page[1])->select();
        if($data){
            $res['stat'] = 1;
            $res['data'] = $data;
            $res['count'] = $data = D($modelName)->where($param)->where(array_merge($search,array('status'=>array('neq',-1))))->scope($scope)->count();
            $res['msg'] = '查找到1条数据。';
        }elseif($data == null){
            $res['stat'] = 1;
            $res['data'] = array();
            $res['count'] = 0;
            $res['msg'] = '查找到0条数据。';
        }else{
            $res['stat'] = 0;
            $res['data'] = $data;
            $res['msg'] = '查找失败！';
        }
        $res['recordsTotal'] = $res['count'];
        $res['recordsFiltered'] = $res['count'];
        $this->ajaxReturn($res);
    }

    public function status(){
        $req = $this->getReq();
        $id = strToWhere($req['id']);
        $status = $req['status'];
        if($id && isset($status) && $status!=''){
            $modelName = $this->modelName;
            $hasDsub = D($modelName)->hasDsub('Zone',array('community_id'=>$id, 'status'=>1));
            if($hasDsub && $status==-1){
                $res['stat'] = 0;
                $res['msg'] = '该元素下有子元素存在，禁止删除！';
            }else{
                $data = D($modelName)->where(array('id'=>$id))->save(array('status'=>$status));
                if($data){
                    if($status == 0){
                        D('Zone')->where(array('community_id'=>$id))->save(array('status'=>$status));
                        D('Building')->where(array('community_id'=>$id))->save(array('status'=>$status));
                        D('Unit')->where(array('community_id'=>$id))->save(array('status'=>$status));
                        D('Room')->where(array('community_id'=>$id))->save(array('status'=>$status));
                        D('Family')->where(array('community_id'=>$id))->save(array('status'=>$status));
                    }
                    $res['stat'] = 1;
                    $res['data'] = $data;
                    $res['msg'] = '更新数据成功。';
                }else{
                    $res['stat'] = 0;
                    $res['msg'] = '更新数据失败！';
                }
            }
        }else{
            $res['stat'] = 0;
            $res['msg'] = '请提交参数！';
        }
        $this->ajaxReturn($res);
    }

    public function depUpdate(){
        $data = M('Community')->select();
//        myDump($data);
        foreach ($data as $v) {
            D('Depart')->addDept($v);
        }
        $depData = D('Depart')->select();

        foreach ($depData as $v) {
            if($v['community_id']){
                $data = array();
                $data['department_id'] = $v['id'];
                $data['id'] = $v['community_id'];
                M('Community')->data($data)->save();
            }
        }
        $this->success('同步成功！');
    }
}